const bannerParent = {
  name: "banner",
  title: "轮播图管理"
}
const proParent = {
  name: "pro",
  title: "产品管理"
}
const routes = [
  {
    path: '/',
    redirect: { name: 'statistic' },
  },
  {
    path: '/statistic',
    name: 'statistic',
    title: "统计",
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppStatistic/index.vue'),
  },
  {
    path: '/banner/list',
    name: 'banner-list',
    parent: bannerParent,
    title: "轮播图列表",
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppBannerManage/AppBannerList/index.vue'),
  },
  {
    path: '/banner/upload',
    name: 'banner-upload',
    parent: bannerParent,
    title: "轮播图上传",
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppBannerManage/AppBannerUpload/index.vue'),
  },
  {
    path: '/vuex/example',
    name: 'vuex-example',
    title: "vuex示例",
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppVuexExample/index.vue'),
  },
  {
    path: '/user',
    name: 'user',
    title: "用户数据",
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppUser/index.vue')
  },
  {
    path: '/pro/list',
    name: 'pro-list',
    title: "用户数据",
    parent: proParent,
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppProManage/AppProList/index.vue')
  },
  {
    path: '/pro/filter',
    name: 'pro-filter',
    title: "用户帅选",
    parent: proParent,
    component: () => import(/* webpackChunkName: "Home" */ '../pages/AppProManage/AppProFilterList/index.vue')
  }
]

// 清洗之后的数据
// [
//   {
//       "path": "/statistic",
//       "name": "statistic",
//       "title": "统计"
//   },
//   {
//       "name": "banner",
//       "title": "轮播图管理",
//       "children": [
//           {
//               "path": "/banner/list",
//               "name": "banner-list",
//               "parent": {
//                   "name": "banner",
//                   "title": "轮播图管理"
//               },
//               "title": "轮播图列表"
//           },
//           {
//               "path": "/banner/upload",
//               "name": "banner-upload",
//               "parent": {
//                   "name": "banner",
//                   "title": "轮播图管理"
//               },
//               "title": "轮播图上传"
//           }
//       ]
//   }
// ]
// 将路由表routes,转换成菜单表menus
export const formatMenusFromRoutes = (routes) => {
  const result = []
  for (let i = 0; i < routes.length; i++) {
    if (!routes[i].title) continue
    if (!routes[i].parent) result.push(routes[i])
    else {
      const parent = result.find(item => item.name === routes[i].parent.name)
      if (parent) {
        parent.children.push(routes[i])
      } else {
        result.push({
          // ...routes[i].parent,
          name: routes[i].parent.name,
          title: routes[i].parent.title,
          children: [routes[i]]
        })
      }
    }
  }
  return result
}

// 渲染菜单的数据
export const menus = formatMenusFromRoutes(routes)
export default routes